import axios from '@/utils/axios'
// import { ADD_CHOPPING_CART_API, CHANGE_CHOPPING_CART_NUM_API, DELETE_CHOPPING_CART_API, ADD_CHOPPING_COLLECT_API, DELETE_CHOPPING_COLLECT_API } from '@/api/mall'
import { Toast } from 'vant'
export default {
    state: {
        shoppingCartList: [],
        shoppingCollectList: []
    },
    mutations: {
        setShoppingCartList(state, list) {
            if (list.constructor == Array) {
                state.shoppingCartList = list.slice()
            }
        },
        setShoppingCollectList(state, list) {
            if (list.constructor == Array) {
                state.shoppingCollectList = list.slice()
            }
        },
        addShoppingCollect(state, data) {
            if (data.id) {
                state.shoppingCollectList.push(data)
            }
        }
    },
    getters: {
        getShoppingCartList(state) {
            return state.shoppingCartList
        },
        getShoppingCollectList(state) {
            return state.shoppingCollectList
        }
    },
    actions: {
        addShopping({ state, commit }, { data, flag }) {
            let goodsId = data.id
            let selectNum = data.selectNum
            let have = false
            // flag = flag === undefined ? null : flag
            flag = flag == undefined ? true : flag
            let shoppingCartList = state.shoppingCartList.filter((item, index) => {
                if (item.id == goodsId) {
                    have = true
                    // item.selectNum = selectNum
                    // if (flag === null) {
                    //     item.selectNum += selectNum
                    // } else {
                    //     if (flag) {
                    //         item.selectNum += 1
                    //     } else {
                    //         item.selectNum -= 1
                    //     }
                    // }
                    if (flag) {
                        item.selectNum += 1
                    } else {
                        item.selectNum -= 1
                    }
                }
                return item
            })
            if (have) {
                Toast.loading({
                    message: '请稍后...',
                    forbidClick: true,
                    loadingType: 'spinner'
                });
                let params = {
                    goodsId,
                    flag
                }
                axios.req({
                    method: 'post',
                    url: CHANGE_CHOPPING_CART_NUM_API,
                    params
                }).then(res => {
                    Toast.clear()
                })
            }
            if (!have) {
                Toast.loading({
                    message: '请稍后...',
                    forbidClick: true,
                    loadingType: 'spinner'
                });
                axios.req({
                    method: 'post',
                    url: ADD_CHOPPING_CART_API,
                    params: {
                        goodsId,
                        // goodsNum
                        goodsNum: selectNum
                    }
                }).then(res => {
                    Toast.clear()
                })
                shoppingCartList.push(data)
            }
            commit('setShoppingCartList', shoppingCartList)
        },
        removeShopping({ state, commit }, data) {
            let goodsId = data.id
            let shoppingCartList = state.shoppingCartList.filter(item => {
                return item.id !== goodsId
            })
            axios.req({
                method: 'post',
                url: DELETE_CHOPPING_CART_API,
                params: {
                    goodsId
                }
            })
            commit('setShoppingCartList', shoppingCartList)
        },
        addShoppingCollect({ commit }, data) {
            let goodsId = data.id
            axios.req({
                method: 'post',
                url: ADD_CHOPPING_COLLECT_API,
                params: {
                    goodsId
                }
            })
            commit('addShoppingCollect', data)
        },
        removeShoppingCollect({ state, commit }, data) {
            let goodsId = data.id
            let shoppingCollectList = state.shoppingCollectList.filter(item => {
                return item.id !== goodsId
            })
            axios.req({
                method: 'post',
                url: DELETE_CHOPPING_COLLECT_API,
                params: {
                    goodsId
                }
            })
            commit('setShoppingCollectList', shoppingCollectList)
        }
    }
}